package com.tesis_2.DA;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tesis_2.MO.MOComentario;

public class DAOComentarios {
	
	public static ArrayList<MOComentario> getComentarioByRestId(Integer restid) throws JSONException {
		//DAORestaurants.lsRest = lsRest;
		
		ArrayList<MOComentario> lsRest = new ArrayList<MOComentario>();
		
	    try{

	    	JSONArray rests = (new JSONReaderArray().execute(DAOUtils.getServer()+ "/commentaries/getrestbyid/"+restid+".json")).get();
	    	
	    	if (rests == null) return lsRest;
	       	    	
	    	for (int i = 0; i < rests.length(); i++) {
	    		JSONObject jsonComent = rests.getJSONObject(i);
	    	    jsonComent = jsonComent.getJSONObject("commentary");
	    	   
	    	    MOComentario objCmt = new MOComentario();
	    	    
	    	    objCmt.setId(jsonComent.getInt("id"));
	    	    objCmt.setCmt(jsonComent.getString("cmt"));
	    	    objCmt.setCreated_at(DAOUtils.parseDateIso(jsonComent.getString("created_at")));
	    	    objCmt.setLocal_id(jsonComent.getInt("local_id"));
	    	    objCmt.setRate((float)jsonComent.getDouble("rate"));
	    	    objCmt.setReservation_id(jsonComent.getInt("reservation_id"));
	    	    objCmt.setRestaurant_id(jsonComent.getInt("restaurant_id"));
	    	    objCmt.setUpdated_at(DAOUtils.parseDateIso(jsonComent.getString("updated_at")));
	    	    objCmt.setUser_id(jsonComent.getInt("user_id"));
	    	    objCmt.setUsrname(jsonComent.getString("usrname"));
	    	    //[{"commentary":{"cmt":"Esto es un comentario de prueba.","created_at":"2012-12-11T22:52:43Z",
	    	    //"id":1,"local_id":2,"rate":3,"reservation_id":1,"restaurant_id":2,
	    	    //"updated_at":"2012-12-11T22:52:43Z","user_id":1,"usrname":"Hunter"}}]
	    	    lsRest.add(objCmt);
	    	}
	 
	    }catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsRest;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return lsRest;
		}
		return lsRest;
		
	}

}
